﻿#if UNITY_EDITOR
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using System.Data.Odbc;

public class LoadMapLaout : MonoBehaviour {

	public static string [,] MapLayout(string SheetName)
	{
		try
		{
			
			string con = "Driver={Microsoft Excel Driver (*.xls)}; HDR=false; DriverId=790; Dbq=" + Application.dataPath + "/../../Tables/MapLayout.xls;";

			Debug.Log(con);

			string yourQuery = "SELECT * FROM [" + SheetName + "$]";
			
			OdbcConnection oCon = new OdbcConnection(con);
			
			OdbcCommand oCmd = new OdbcCommand(yourQuery, oCon);
			
			DataTable dtYourData = new DataTable("YourData");
			
			oCon.Open();
			
			OdbcDataReader rData = oCmd.ExecuteReader();
			
			dtYourData.Load(rData);
			
			rData.Close();

			oCon.Close();
			
			glog.debug("column="+dtYourData.Columns.Count+"  row="+dtYourData.Rows.Count);
			string [,] lines = new string[dtYourData.Columns.Count, dtYourData.Rows.Count+1 ];
				
			for (int x = 0; x < dtYourData.Columns.Count; x++)
			{
				if (dtYourData.Columns[x] != null)
				{
					string s = dtYourData.Columns[x].ToString();
					if (!String.IsNullOrEmpty(s))
					{
				//		Debug.Log("X:" + (x+1) + ", Y:1" + ",    " + s.ToString());
						//if(s.StartsWith("road"))
						if(!s.StartsWith("F"))
							lines[x,0] = s;
					}
				}
			}

			for (int y = 0; y < dtYourData.Rows.Count; y++)
			{
				for (int x = 0; x < dtYourData.Rows[y].ItemArray.Length; x++)
				{
					if (dtYourData.Rows[y][x] != null)
					{
						string s = dtYourData.Rows[y][x].ToString();
						if (!String.IsNullOrEmpty(s))
						{
				//			Debug.Log("X:" + (x+1) + ", Y:" + (y + 2) + ", " + s.ToString());
							//if(s.StartsWith("road"))
							if(!s.StartsWith("F"))
								lines[x,y + 1] = s;
						}
					}
				}
			}
			
			return lines;
		}
		catch (Exception e)
		{
			Debug.LogError(e.ToString());
			return null;
		}
	}
}
#endif